﻿// https://leetcode-cn.com/problems/sum-of-left-leaves/
// 404. 左叶子之和

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <limits.h>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int sum = 0;
    int sumOfLeftLeaves(TreeNode* root) {
        if (root == nullptr) return 0;
        FindLeftVal(root, -1);
        return sum;
    }

    void FindLeftVal(TreeNode *root, int flag) {
        if (root == nullptr) return;

        if (root->left == nullptr && root->right == nullptr && flag == 1) sum += root->val;
        else FindLeftVal(root->left, 1);

        if (root->right) FindLeftVal(root->right, 0);
    }
};

int main() {
    Solution obj = Solution();
}